package test.Channel.udp;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.CharBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

public class UDPClient {
	public static void main(String[] args) {
		DatagramChannel socket = null;
		try {
			// 创建Socket
			socket = DatagramChannel.open();
			InetSocketAddress ip = new InetSocketAddress("localhost", 12345);
			socket.connect(ip);

			// 发送数据
			CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
			socket.write(encoder.encode(CharBuffer.wrap("Hello")));
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭
			try {
				socket.close();
			} catch (IOException e) {
			}
		}

	}
}
